/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MODEL.CONTROLLER;

import MODEL.DAO.ChiTietSanPhamDAO;
import MODEL.DAO.TinTucDAO;
import MODEL.POJO.ChiTietSanPhamPOJO;
import MODEL.POJO.TinTucPOJO;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author kijoo
 */
@WebServlet(name = "AdminController", urlPatterns = {"/AdminController"})
public class AdminController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        String action = request.getParameter("Action");
        if (session.getAttribute("Admins") != null) {
            if (action != null) {
                if (action.equals("View")) {
                    ArrayList<TinTucPOJO> dstt = TinTucDAO.LayTinMoiXuatHien();
                    request.setAttribute("DanhSachTinTuc", dstt);
                    request.getRequestDispatcher("/Admin.jsp").forward(request, response);
                }
                if (action.equals("ThemTinTuc")) {
                    ArrayList<TinTucPOJO> dstt = TinTucDAO.LayTinMoiXuatHien();
                    request.setAttribute("DanhSachTinTuc", dstt);
                    request.getRequestDispatcher("/ThemTinTuc.jsp").forward(request, response);
                }
                
                if (action.equals("ThemLoaiDanhMuc")) {
                    ArrayList<TinTucPOJO> dstt = TinTucDAO.LayTinMoiXuatHien();
                    request.setAttribute("DanhSachTinTuc", dstt);
                    request.getRequestDispatcher("/ThemLoaiDanhMuc.jsp").forward(request, response);
                }
                if (action.equals("ThemDanhMuc")) {
                    ArrayList<TinTucPOJO> dstt = TinTucDAO.LayTinMoiXuatHien();
                    request.setAttribute("DanhSachTinTuc", dstt);
                    request.getRequestDispatcher("/ThemDanhMuc.jsp").forward(request, response);
                }
                if (action.equals("ThemSanPham")) {
                    ArrayList<TinTucPOJO> dstt = TinTucDAO.LayTinMoiXuatHien();
                    request.setAttribute("DanhSachTinTuc", dstt);
                    request.getRequestDispatcher("/ThemSanPham.jsp").forward(request, response);
                }
                //Quan ly san pham Admin
                if (action.equals("XemDanhSachSP")) {
                    String yeucau = "";
                    int trang = 1;
                    String tenloaisanpham = request.getParameter("tenloaisanpham");
                    if (tenloaisanpham == null) {
                        tenloaisanpham = (String) request.getParameter("idloaisanpham");
                    }
                    request.setAttribute("TenLoaiSanPham", tenloaisanpham);
                    String idloaisanpham = request.getParameter("idloaisanpham");
                    request.setAttribute("IDLoaiSanPham", idloaisanpham);

                    ArrayList<ChiTietSanPhamPOJO> dsctsp = ChiTietSanPhamDAO.LayDSTheoLSPCoPhanTrang(idloaisanpham, trang, yeucau);
                    int soluongtrang = ChiTietSanPhamDAO.tinhsoluongtrang5(idloaisanpham);
                    int xuly = 5;
                    ArrayList<TinTucPOJO> dstt = TinTucDAO.LayTinMoiXuatHien();
                    request.setAttribute("DanhSachTinTuc", dstt);
                    request.setAttribute("XyLy", xuly);
                    request.setAttribute("SoTrang", soluongtrang);
                    request.setAttribute("YeuCau", yeucau);
                    request.setAttribute("DanhSachCTSPs", dsctsp);
                    request.getRequestDispatcher("/SanPhamAdmin.jsp").forward(request, response);
                }
                if (action.equals("Sua")) {
                    String yeucau = "";
                    int trang = Integer.parseInt(request.getParameter("Trang"));
                    String tenloaisanpham = request.getParameter("tenloaisanpham");
                    request.setAttribute("TenLoaiSanPham", tenloaisanpham);
                    String idloaisanpham = request.getParameter("idloaisanpham");
                    request.setAttribute("IDLoaiSanPham", idloaisanpham);
                    ArrayList<ChiTietSanPhamPOJO> dsctsp = ChiTietSanPhamDAO.LayDSTheoLSPCoPhanTrang(idloaisanpham, trang, yeucau);
                    int soluongtrang = ChiTietSanPhamDAO.tinhsoluongtrang5(idloaisanpham);
                    int xuly = 5;
                    ArrayList<TinTucPOJO> dstt = TinTucDAO.LayTinMoiXuatHien();
                    request.setAttribute("DanhSachTinTuc", dstt);
                    request.setAttribute("XyLy", xuly);
                    request.setAttribute("SoTrang", soluongtrang);
                    request.setAttribute("YeuCau", yeucau);
                    request.setAttribute("DanhSachCTSPs", dsctsp);
                    int idchitietsanpham = Integer.parseInt(request.getParameter("idchitietsanpham"));
                    request.setAttribute("IDChiTietSanPhamSua", idchitietsanpham);
                    ChiTietSanPhamPOJO ctsp = ChiTietSanPhamDAO.LayChiTietSanPham(idchitietsanpham);
                    request.setAttribute("ChiTietSanPhamSua", ctsp);
                    request.setAttribute("GiaTriSua", "1");
                    request.getRequestDispatcher("/SanPhamAdmin.jsp").forward(request, response);

                }
                if (action.equals("Xoa")) {
                    String yeucau = "";
                    int trang = Integer.parseInt(request.getParameter("Trang"));
                    String tenloaisanpham = request.getParameter("tenloaisanpham");
                    request.setAttribute("TenLoaiSanPham", tenloaisanpham);
                    String idloaisanpham = request.getParameter("idloaisanpham");
                    request.setAttribute("IDLoaiSanPham", idloaisanpham);
                    ArrayList<ChiTietSanPhamPOJO> dsctsp = ChiTietSanPhamDAO.LayDSTheoLSPCoPhanTrang(idloaisanpham, trang, yeucau);
                    int soluongtrang = ChiTietSanPhamDAO.tinhsoluongtrang5(idloaisanpham);
                    int xuly = 5;
                    ArrayList<TinTucPOJO> dstt = TinTucDAO.LayTinMoiXuatHien();
                    request.setAttribute("DanhSachTinTuc", dstt);
                    request.setAttribute("XyLy", xuly);
                    request.setAttribute("SoTrang", soluongtrang);
                    request.setAttribute("YeuCau", yeucau);
                    request.setAttribute("DanhSachCTSPs", dsctsp);
                    int idchitietsanpham = Integer.parseInt(request.getParameter("idchitietsanpham"));
                    boolean kq = ChiTietSanPhamDAO.XoaSanPham(idchitietsanpham);
                    if (kq == true) {
                        request.setAttribute("GiaTriSua", "0");
                        response.sendRedirect("AdminController?Action=XemDanhSachSP&idloaisanpham=" + idloaisanpham + "&tenloaisanpham=" + tenloaisanpham);
                        //request.getRequestDispatcher("/SanPhamAdmin.jsp").forward(request, response);
                    }
                }
                //Quan ly tin tuc
                if (action.equals("XemDanhSachTinTuc")) {
                    ArrayList<TinTucPOJO> dstt = TinTucDAO.LayTinMoiXuatHien();
                    request.setAttribute("DanhSachTinTuc", dstt);
                    request.getRequestDispatcher("/QuanLyTinTuc.jsp").forward(request, response);
                }
                if (action.equals("SuaTin")) {
                    String idtin = request.getParameter("idtin");
                    TinTucPOJO tt = TinTucDAO.LayChiTietTin(idtin);
                    request.setAttribute("ChiTietTinTucSua", tt);
                    ArrayList<TinTucPOJO> dstt = TinTucDAO.LayTinMoiXuatHien();
                    request.setAttribute("DanhSachTinTuc", dstt);
                    request.setAttribute("GiaTriSua", "1");
                    request.getRequestDispatcher("/QuanLyTinTuc.jsp").forward(request, response);
                }
                if (action.equals("XoaTin")) {
                    String idtin = request.getParameter("idtin");
                    boolean kq = TinTucDAO.XoaTinTuc(idtin);
                    if (kq == true) {
                        request.setAttribute("GiaTriSua", "0");
                        response.sendRedirect("AdminController?Action=XemDanhSachTinTuc");
                    }
                }
            } else {
                if (request.getParameter("btSua") != null) {
                    int idchitietSP = Integer.parseInt(request.getParameter("txtIDChiTietSP"));
                    String tenSP = request.getParameter("txtTen");
                    String motaSP = request.getParameter("txtMoTa");
                    String hinh = request.getParameter("txtHinh");
                    BigDecimal giaban = new BigDecimal(request.getParameter("txtGia"));
                    int soluong = Integer.parseInt(request.getParameter("txtSoLuong"));
                    int baohanh = Integer.parseInt(request.getParameter("txtBaoHanh"));
                    ArrayList<TinTucPOJO> dstt = TinTucDAO.LayTinMoiXuatHien();
                    request.setAttribute("DanhSachTinTuc", dstt);

                    boolean kq = ChiTietSanPhamDAO.CapNhatSanPham(idchitietSP, tenSP, motaSP, hinh, giaban, soluong, baohanh);
                    if (kq == true) {
                        String idloaisanpham = request.getParameter("txtIDLoaiSP");
                        request.setAttribute("idloaisanphamSua", idloaisanpham);
                        String tenloaisanpham = request.getParameter("txtTenLoaiSP");
                        request.setAttribute("tenloaisanpham", tenloaisanpham);
                        request.setAttribute("GiaTriSua", "0");
                        response.sendRedirect("AdminController?Action=XemDanhSachSP&idloaisanpham=" + idloaisanpham + "&tenloaisanpham=" + tenloaisanpham);
                        return;
                    } else {
                        request.setAttribute("GiaTriSua", "1");
                        response.sendRedirect("AdminController?Action=XemDanhSachSP");
                    }
                }
                if (request.getParameter("btSuaTin") != null) {
                    String idtin = request.getParameter("idtin");
                    String ten = request.getParameter("ten");
                    String hinh = request.getParameter("hinh");
                    String tomtat = request.getParameter("tomtat");
                    String noidung = request.getParameter("noidung");
                    String ghichu = request.getParameter("ghichu");
                    boolean kq = TinTucDAO.CapNhatTinTuc(idtin, ten, hinh, tomtat, noidung, ghichu);
                    if (kq == true) {
                        ArrayList<TinTucPOJO> dstt = TinTucDAO.LayTinMoiXuatHien();
                        request.setAttribute("DanhSachTinTuc", dstt);
                        request.setAttribute("GiaTriSua", "0");
                        response.sendRedirect("AdminController?Action=XemDanhSachTinTuc");
                        return;
                    } else {
                        request.setAttribute("GiaTriSua", "1");
                        response.sendRedirect("AdminController?Action=XemDanhSachTinTuc");
                    }
                }

            }

        } else {
            request.getRequestDispatcher("/Admin.jsp").forward(request, response);
            return;
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
